/*
 * Java CMPP API
 * Copyright (C) 1998 - 2002 by Xu Youming
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html
 * Java CMPP API author: xuym@bc-info.net
 * Java CMPP API Homepage: http://cmppapi.sourceforge.net/
 * $Id: PacketFactory.java,v 1.1 2008/03/19 02:56:56 xym Exp $
 */
package com.bci.cmpp.util;

import com.bci.cmpp.BadCommandIDException;
import com.bci.cmpp.CMPPConstants;
import com.bci.cmpp.message.*;

/**
 * Helper class to create new CMPP packet objects.
 * 
 * @since 1.0
 * @author Xu Youming
 */
public class PacketFactory {
	private PacketFactory() {
	}

	/**
	 * Create a new instance of the appropriate sub class of CMPPPacket.
	 * 
	 * @deprecated
	 */
	// public static CMPPPacket newPacket(int id) throws BadCommandIDException {
	// return (newInstance(id));
	// }
	/**
	 * Create a new instance of the appropriate sub class of CMPPPacket. Packet
	 * fields are all left at their default initial state.
	 * 
	 * @param id
	 *            The CMPP command ID of the packet type to return.
	 * @return A sub-class instance of {@link com.bci.cmpp.message.CMPPPacket}
	 *         representing CMPP command <code>id</code>.
	 * @throws com.bci.cmpp.BadCommandIDException
	 *             if the command ID is not recognized.
	 */

	public static CMPPPacket newInstance(int id, byte ver)
			throws BadCommandIDException {
		CMPPPacket response = null;

		switch (id) {

		case CMPPConstants.CMPP_CONNECT:
			response = new CMPPConnect();
			break;

		case CMPPConstants.CMPP_CONNECT_RESP:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPConnectResp();
			else
				response = new CMPPConnectResp30();
			break;

		case CMPPConstants.CMPP_TERMINATE:
			response = new CMPPTerminate();
			break;

		case CMPPConstants.CMPP_TERMINATE_RESP:
			response = new CMPPTerminateResp();
			break;

		case CMPPConstants.CMPP_SUBMIT:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPSubmit();
			else
				response = new CMPPSubmit30();
			break;

		// case CMPPConstants.CMPP_SUBMIT30:
		// response = new CMPPSubmit30();
		// break;
		case CMPPConstants.CMPP_SUBMIT_RESP:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPSubmitResp();
			else
				response = new CMPPSubmitResp30();
			break;

		// case CMPPConstants.DATA_SM:
		// response = new DataSM();
		// break;

		// case CMPPConstants.DATA_SM_RESP:
		// response = new DataSMResp();
		// break;

		// case CMPPConstants.ALERT_NOTIFICATION:
		// response = new AlertNotification();
		// break;

		// case CMPPConstants.CMPP_SUBMIT_MULTI:
		// response = new SubmitMulti();
		// break;

		// case CMPPConstants.CMPP_SUBMIT_MULTI_RESP:
		// response = new SubmitMultiResp();
		// break;

		case CMPPConstants.CMPP_DELIVER:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPDeliver();
			else
				response = new CMPPDeliver30();
			break;

		// case CMPPConstants.CMPP_DELIVER30:
		// response = new CMPPDeliver30();
		// break;

		case CMPPConstants.CMPP_DELIVER_RESP:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPDeliverResp();
			else
				response = new CMPPDeliverResp30();
			break;

		case CMPPConstants.CMPP_QUERY:
			response = new CMPPQuery();
			break;

		case CMPPConstants.CMPP_QUERY_RESP:
			response = new CMPPQueryResp();
			break;

		case CMPPConstants.CMPP_CANCEL:
			response = new CMPPCancel();
			break;

		case CMPPConstants.CMPP_CANCEL_RESP:
			if (ver == CMPPConstants.CMPP_VERSION_2)
				response = new CMPPCancelResp();
			else
				response = new CMPPCancelResp30();
			break;

		case CMPPConstants.CMPP_ACTIVE_TEST:
			response = new CMPPActiveTest();
			break;

		case CMPPConstants.CMPP_ACTIVE_TEST_RESP:
			response = new CMPPActiveTestResp();
			break;

		default:
			throw new BadCommandIDException("Unrecognised command:" + id);
		}
		return (response);
	}
}